'use strict';

angular.module('axonBank')
        .controller('BankAccountsCtrl', function ($scope, $uibModal, BankAccountService) {
            function updateBankAccounts(bankAccounts) {
                alert("BankAccountsCtrl --updateBankAccounts")
                $scope.bankAccounts = bankAccounts;
            }

            $scope.create = function () {
                alert("create account");
                $uibModal.open({
                    controller: 'CreateBankAccountModalCtrl',
                    templateUrl: '/app/modals/createBankAccountModal.html',
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body'
                });
            };

            $scope.deposit = function (id) {
                alert("deposit:" + id);
                $uibModal.open({
                    controller: 'DepositMoneyModalCtrl',
                    templateUrl: '/app/modals/depositMoneyModal.html',
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    resolve: {
                        bankAccountId: function () {
                            return id;
                        }
                    }
                });
            };

            $scope.withdraw = function (id) {
                alert("withdraw:" + id);
                $uibModal.open({
                    controller: 'WithdrawMoneyModalCtrl',
                    templateUrl: '/app/modals/withdrawMoneyModal.html',
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    resolve: {
                        bankAccountId: function () {
                            return id;
                        }
                    }
                });
            };

            $scope.transfer = function (id) {
                alert("transfer:" + id);
                $uibModal.open({
                    controller: 'TransferMoneyModalCtrl',
                    templateUrl: '/app/modals/transferMoneyModal.html',
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    resolve: {
                        bankAccountId: function () {
                            return id;
                        },
                        bankAccounts: function () {
                            return $scope.bankAccounts;
                        }
                    }
                });
            };

            $scope.bankTransfers = function (id) {
                alert("bankTransfers:" + id);
                $uibModal.open({
                    controller: 'BankTransfersModalCtrl',
                    templateUrl: '/app/modals/bankTransfersModal.html',
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    resolve: {
                        bankAccountId: function () {
                            return id;
                        },
                        bankTransfers: function () {
                            return BankAccountService.loadBankTransfers(id);
                        }
                    }
                });
            };

            BankAccountService.connect()
                    .then(function () {
                         alert("connect in controller");
                        BankAccountService.loadBankAccounts()
                                .then(updateBankAccounts);

                        BankAccountService.subscribeToBankAccountUpdates()
                                .then(function () {
                                    // do nothing
                            alert("subscribeToBankAccountUpdates");
                                }, function () {
                                    // do nothing
                                }, updateBankAccounts)
                    });
        })
        .controller('CreateBankAccountModalCtrl', function ($uibModalInstance, $scope, BankAccountService) {
            alert("CreateBankAccountModalCtrl controller");
            $scope.bankAccount = {};

            $scope.cancel = function () {
                $uibModalInstance.dismiss();
            };
            $scope.submit = function () {
                BankAccountService.createBankAccount($scope.bankAccount);
                $uibModalInstance.close();
            };
        })
        .controller('DepositMoneyModalCtrl', function ($uibModalInstance, $scope, BankAccountService, bankAccountId) {
            alert("DepositMoneyModalCtrl controller");
            $scope.deposit = {
                bankAccountId: bankAccountId
            };

            $scope.cancel = function () {
                $uibModalInstance.dismiss();
            };
            $scope.submit = function () {
                BankAccountService.deposit($scope.deposit);
                $uibModalInstance.close();
            };
        })
        .controller('WithdrawMoneyModalCtrl', function ($uibModalInstance, $scope, BankAccountService, bankAccountId) {
            alert("WithdrawMoneyModalCtrl controller");
            $scope.withdrawal = {
                bankAccountId: bankAccountId
            };

            $scope.cancel = function () {
                $uibModalInstance.dismiss();
            };
            $scope.submit = function () {
                BankAccountService.withdraw($scope.withdrawal);
                $uibModalInstance.close();
            };
        })
        .controller('TransferMoneyModalCtrl',
                function ($uibModalInstance, $scope, BankAccountService, bankAccountId, bankAccounts) {
                    alert("TransferMoneyModalCtrl controller");
                    $scope.bankAccounts = bankAccounts;
                    $scope.bankTransfer = {
                        sourceBankAccountId: bankAccountId
                    };

                    $scope.cancel = function () {
                        $uibModalInstance.dismiss();
                    };
                    $scope.submit = function () {
                        BankAccountService.transfer($scope.bankTransfer);
                        $uibModalInstance.close();
                    };
                })
        .controller('BankTransfersModalCtrl',
                function ($uibModalInstance, $scope, BankAccountService, bankAccountId, bankTransfers) {
                    alert("BankTransfersModalCtrl controller");
                    $scope.bankAccountId = bankAccountId;
                    $scope.bankTransfers = bankTransfers;

                    $scope.close = function () {
                        $uibModalInstance.close();
                    };
                });